package com.wei.czz.framework.common.controller;

import com.wei.czz.common.annotation.Resubmit;
import com.wei.czz.common.enums.index.validateCode.ValidateCodeTypeEnum;
import com.wei.czz.common.tool.Result;
import com.wei.czz.common.utils.RegexUtils;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2022-04-26 19:52:56
 * className: SmsController 短信服务请求接口类
 * version: 1.0
 * description:
 */
@RestController
@RequestMapping("index/sms")
@AllArgsConstructor
public class SmsController {

    private static final Logger log = LoggerFactory.getLogger(SmsController.class);

    /**
     * 发送短信接口
     * <p>
     * 调用方：
     *  1、浏览器-登录/注册页面
     *  2、浏览器-后台管理页-用户个人信息管理页面调用
     * <p>
     * 此接口会校验是否重复提交
     * @param phone        手机号
     * @param validateType 验证类型
     * @param request http请求对象
     * @return 请求结果
     */
    @Resubmit(message = "发送短信")
    @GetMapping("/send")
    public Result<Object> sendSmsCode(@RequestParam("phone") String phone, @RequestParam("validateType") Integer validateType,
                                       HttpServletRequest request) {
        log.info("开始发送短信业务。phone={} validateType={}", phone, validateType);

        if (StringUtils.isBlank(phone) || !RegexUtils.isPhone(phone)) {
            log.info("发送短信接口请求参数错误");
            return Result.paramFail();
        }
        if (ValidateCodeTypeEnum.checkPhoneCodeType(validateType)) {
            log.info("发送短信接口，验证类型参数错误");
            return Result.paramFail();
        }

//        czzSmsService.sendSmsCode(phone, validateType, request);

        log.info("发送短信业务结束");
        return Result.success();
    }
}
